package no.progark.golf.activities;

import java.util.ArrayList;

import no.progark.golf.Golf;
import no.progark.golf.Map;
import no.progark.golf.Player;
import no.progark.golf.R;
import no.progark.golf.views.SettingsView;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class GameSettingsActivity extends ListActivity implements OnItemSelectedListener {
	public Golf game;
	private SettingsView settingsView;
	private ArrayList<Player>  p = new ArrayList<Player>();
	private ArrayList<Map> m = new ArrayList<Map>();
	private ArrayList<TextView> rows = new ArrayList<TextView>();
	private int selectedPlayer = -1;
	private View selectedView;
	private AdapterView<?> parent;
	ListView lv;
	
	
	
	public GameSettingsActivity getThis(){
		return this;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.gamesettings);
		p.add(new Player("Player 1"));
		p.add(new Player("Player 2"));
				
		//REMOVE PLAYERS
		final Button removePlayerButton = (Button) findViewById(R.id.removeplayer);
		removePlayerButton.setEnabled(false);
		
		//INIT ALL MAPS
		Map.initAllMaps();
		
		/*
		 * ADDPLAYERS
		 */
		final Button addPlayerButton = (Button) findViewById(R.id.addplayer);
		
		/*
		 * LIST
		 */
		lv = getListView();
		ArrayAdapter<Player> adapter1 = new ArrayAdapter<Player>(this,android.R.layout.simple_list_item_1, p);
		lv.setAdapter(adapter1);
		lv.invalidateViews();
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> par, View view, int pos, long id) {
				removePlayerButton.setEnabled(true);
				if(selectedPlayer != -1){
					deselectPrevious(lv, selectedPlayer, selectedView, par);	
				}
				View selected = lv.getAdapter().getView(pos, view, par);
				
				
				selectedPlayer = pos;
				selectedView = view;
				parent = par;
				System.out.println("new pos: " + selectedPlayer);
				
				selected.setBackgroundColor(Color.parseColor("#AA5500"));
				
				
			}
		});
		
	    
	    /*
	     * MAPSPINNER
	      */
	    Spinner mapSpinner = (Spinner) findViewById(R.id.mapSpinner);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.maps_array, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    mapSpinner.setAdapter(adapter);

	    mapSpinner.setOnItemSelectedListener(this);
	    
	    
	    /*
	     * STARTGAME
	     */
	    final Button startGameButton = (Button) findViewById(R.id.startGameButton);
	    startGameButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				if(canStartGame()){
					game.initPlayerAndMapLists(p, m);
					game.initializeGame();
					//START!//
					Intent i = new Intent(getBaseContext(), GameActivity.class);
					startActivity(i);
					finish();
				}
			}
		});
	    
	    addPlayerButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				final Dialog dia = new Dialog(GameSettingsActivity.this);
				dia.setContentView(R.layout.playername);
				dia.setCancelable(true);
				
				Button cancelButton = (Button) dia.findViewById(R.id.CancelAddButton);
				Button okButton = (Button) dia.findViewById(R.id.FinalAddPlayerButton);
				
				cancelButton.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						dia.dismiss();
						
					}
				});
				
				okButton.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						TextView name = (TextView) dia.findViewById(R.id.editText1);
						String name1 = name.getText().toString();
//						System.out.println(name1);
						Player newestP = new Player(name.getText().toString());
						p.add(newestP);
						lv.invalidateViews();
//						TextView row = findNextOpenSlot();
//						row.setText(newestP.getName());
						
						
						if(p.size()>= game.MAXPLAYERS)addPlayerButton.setEnabled(false);
						if(p.size() > 1)startGameButton.setEnabled(true);
						dia.dismiss();
						
						
						
					}

					
				});
				dia.show();
				
			}
		});
	    
	    removePlayerButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				System.out.println("selected: " + selectedPlayer);
				deselectPrevious(lv, selectedPlayer, selectedView, parent);
				p.remove(selectedPlayer);
				removePlayerButton.setEnabled(false);
				addPlayerButton.setEnabled(true);
				if(lv.getAdapter().getCount()<2)startGameButton.setEnabled(false);
				
				lv.invalidateViews();
			}
			
		});
	    
	    
	    /*
	     * BACK
	     */
	    Button backButton = (Button) findViewById(R.id.backButton);
	    backButton.setOnClickListener(new View.OnClickListener() {
	    	public void onClick(View v) {
	    		Intent i = new Intent(getBaseContext(), MainMenuActivity.class);
				startActivity(i);
	    	}
	    });
	    
	    
//	    
//	    TableRow tablerow = (TableRow)findViewById(R.id.tr1);
//	    tablerow.setOnClickListener(new  OnClickListener() {
//	    	
//	    	
//	        public void onClick(View v) {
//	            v.setBackgroundColor(Color.GRAY);
//	            selectedPlayer = 0;
//	            removePlayerButton.setEnabled(true);
//	            // TODO Auto-generated method stub
//
//	        }
//	    });
	    
	    
	}
	
   


	
	
	protected void deselectPrevious(ListView lv2, int pos, View view,
			AdapterView<?> parent) {
		if(pos < lv2.getAdapter().getCount()){
		View prev = lv2.getAdapter().getView(pos, view, parent);
		System.out.print("Before - bgc");
		prev.setBackgroundColor(Color.parseColor("gray"));
		System.out.println("----After - bgc");
		}
		System.out.println("deselect done, old pos was:" + pos);
		
	}
	
	@Override
	public void onBackPressed() {
		game.reset();
		Intent i = new Intent(getBaseContext(), MainMenuActivity.class);
		startActivity(i);
		super.onBackPressed();
	}

	
	
	 

	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//		Toast.makeText(parent.getContext(), "Maps to be played: " +
//				parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
		setMaps( Integer.parseInt(parent.getItemAtPosition(pos).toString()));
	}
	
	public void onNothingSelected(AdapterView parent) {
		// Do nothing.
	}
	 
	public GameSettingsActivity(){
		this.game = Golf.getInstance();
	}
	
	public void addPlayer(Player player){
		p.add(player);
	}
	
	public void removePlayer(Player player){
		p.remove(player);
	}
	
	
	public void removeSelectedPlayer(){
		
	}
	
	private void setMaps(int n){
		m.clear();
		for (int i = 0; i < n; i++) {
			m.add(Map.getAllMaps().get(i));
		}
		
	}
	
	
	/**
	 * Checks if there are enough players and maps to start a game. Used when "start-game" button is pressed 
	 * @return true if new game can be instantiated
	 */
	private boolean canStartGame(){
		return (p.size() > 1 && m.size() > 0)?true:false;
	}

}
